package b;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

import static MyUtils.MHUtils.*;
import static MyUtils.MHMoban.*;

/*
    每个节点只处理自己（单cur思想）
    做完题一定写入表格
*/
public class IP地址 {
    @Test
    public void test() {
        System.out.println("RES:" + restoreIpAddresses("25525511135"));
    }

    String s;
    List<String> res = new ArrayList<>();
    StringBuilder tmp = new StringBuilder();
    List<String> restoreIpAddresses(String S){
        s=S;
        //有#@,就不可能
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if(c[i]<'0'||c[i]>'9')return res;
        }

        int cI=0,duan=1;
        bT(cI,duan);
        return res;
    }

    void bT(int cI, int duan){
        if(duan==4+1 || cI==s.length()){
            if(duan==4+1 && cI==s.length())res.add(tmp.toString().substring(0,tmp.length()-1));//去掉末尾点.
            return;
        }
        for (int i=1; i<=3; i++) {
            if(cI+i>s.length())continue;
            String sub = s.substring(cI, cI + i);
            if(!fuhe(sub))continue;
            tmp.append(sub+"."); bT(cI+i,duan+1); tmp.delete(tmp.length()-i-1,tmp.length());//-1是去掉点.
        }
    }

    boolean fuhe(String sub) {
        //258
        int val = Integer.parseInt(sub);
        if(val>255)return false;
        //0.可以 010.不行
        char fir = sub.charAt(0);
        if(sub.length()==1 && fir=='0') return true;
        else if(sub.length()>1 && fir=='0') return false;//只要长度不是1 ‘00 010 只要第一个是0都错’
        return true;
    }
}
